import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;

public class talkSever {

    //监听端口
    private Integer port;

    public talkSever(Integer port) {

        this.port = port;
    }
    //编写run方法，处理客户端的请求
    public void run() throws InterruptedException {
        //创建两个线程组
        NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            //创建workerGroup启动类
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap
                    .group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG,128)
                    .childOption(ChannelOption.SO_KEEPALIVE,true)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipeline = ch.pipeline();
                            //向pipeline里加入解码器
                            pipeline.addLast("decoder",new StringDecoder());
                            //向pipeline里加入编码器
                            pipeline.addLast("encoder",new StringEncoder());
                            //向pipeline里加入自定义业务处理器//工作流
                            pipeline.addLast(new talkSeverHandler());
                        }
                    });
            System.out.println("netty server start");
            //绑定端口
            ChannelFuture channelFuture = serverBootstrap.bind(port).sync();

            //添加监听器
            channelFuture.addListener(new GenericFutureListener<Future<? super Void>>() {
                @Override
                public void operationComplete(Future<? super Void> future) throws Exception {
                    if (channelFuture.isSuccess()){
                        System.out.println("server is startting...");
                    }else if (channelFuture.isDone()){
                        System.out.println("server started success...");
                    }
                }
            });

            //监听关闭通道
            channelFuture.channel().closeFuture().sync();

            //添加监听器
            channelFuture.addListener(new GenericFutureListener<Future<? super Void>>() {
                @Override
                public void operationComplete(Future<? super Void> future) throws Exception {
                    if (channelFuture.isCancelled()){
                        System.out.println("server is stopping...");
                    }else if (channelFuture.isCancellable()){
                        System.out.println("server was stopped...");
                    }
                }
            });

        }finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        new talkSever(9999).run();
    }
}
